{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# Read the data\n",
    "data = pd.read_csv('../input/melbourne-housing-snapshot/melb_data.csv')\n",
    "\n",
    "# Separate target from predictors\n",
    "y = data.Price\n",
    "X = data.drop(['Price'], axis=1)\n",
    "\n",
    "# Divide data into training and validation subsets\n",
    "X_train_full, X_valid_full, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2,\n",
    "                                                                random_state=0)\n",
    "\n",
    "# \"Cardinality\" means the number of unique values in a column\n",
    "# Select categorical columns with relatively low cardinality (convenient but arbitrary)\n",
    "categorical_cols = [cname for cname in X_train_full.columns if X_train_full[cname].nunique() < 10 and\n",
    "                    X_train_full[cname].dtype == \"object\"]\n",
    "\n",
    "# Select numerical columns\n",
    "numerical_cols = [cname for cname in X_train_full.columns if X_train_full[cname].dtype in ['int64', 'float64']]\n",
    "\n",
    "# Keep selected columns only\n",
    "my_cols = categorical_cols + numerical_cols\n",
    "X_train = X_train_full[my_cols].copy()\n",
    "X_valid = X_valid_full[my_cols].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "      Type Method             Regionname  Rooms  Distance  Postcode  Bedroom2  \\\n12167    u      S  Southern Metropolitan      1       5.0    3182.0       1.0   \n6524     h     SA   Western Metropolitan      2       8.0    3016.0       2.0   \n8413     h      S   Western Metropolitan      3      12.6    3020.0       3.0   \n2919     u     SP  Northern Metropolitan      3      13.0    3046.0       3.0   \n6043     h      S   Western Metropolitan      3      13.3    3020.0       3.0   \n\n       Bathroom  Car  Landsize  BuildingArea  YearBuilt  Lattitude  \\\n12167       1.0  1.0       0.0           NaN     1940.0  -37.85984   \n6524        2.0  1.0     193.0           NaN        NaN  -37.85800   \n8413        1.0  1.0     555.0           NaN        NaN  -37.79880   \n2919        1.0  1.0     265.0           NaN     1995.0  -37.70830   \n6043        1.0  2.0     673.0         673.0     1970.0  -37.76230   \n\n       Longtitude  Propertycount  \n12167    144.9867        13240.0  \n6524     144.9005         6380.0  \n8413     144.8220         3755.0  \n2919     144.9158         8870.0  \n6043     144.8272         4217.0  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Type</th>\n      <th>Method</th>\n      <th>Regionname</th>\n      <th>Rooms</th>\n      <th>Distance</th>\n      <th>Postcode</th>\n      <th>Bedroom2</th>\n      <th>Bathroom</th>\n      <th>Car</th>\n      <th>Landsize</th>\n      <th>BuildingArea</th>\n      <th>YearBuilt</th>\n      <th>Lattitude</th>\n      <th>Longtitude</th>\n      <th>Propertycount</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>12167</th>\n      <td>u</td>\n      <td>S</td>\n      <td>Southern Metropolitan</td>\n      <td>1</td>\n      <td>5.0</td>\n      <td>3182.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>NaN</td>\n      <td>1940.0</td>\n      <td>-37.85984</td>\n      <td>144.9867</td>\n      <td>13240.0</td>\n    </tr>\n    <tr>\n      <th>6524</th>\n      <td>h</td>\n      <td>SA</td>\n      <td>Western Metropolitan</td>\n      <td>2</td>\n      <td>8.0</td>\n      <td>3016.0</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n      <td>193.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>-37.85800</td>\n      <td>144.9005</td>\n      <td>6380.0</td>\n    </tr>\n    <tr>\n      <th>8413</th>\n      <td>h</td>\n      <td>S</td>\n      <td>Western Metropolitan</td>\n      <td>3</td>\n      <td>12.6</td>\n      <td>3020.0</td>\n      <td>3.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>555.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>-37.79880</td>\n      <td>144.8220</td>\n      <td>3755.0</td>\n    </tr>\n    <tr>\n      <th>2919</th>\n      <td>u</td>\n      <td>SP</td>\n      <td>Northern Metropolitan</td>\n      <td>3</td>\n      <td>13.0</td>\n      <td>3046.0</td>\n      <td>3.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>265.0</td>\n      <td>NaN</td>\n      <td>1995.0</td>\n      <td>-37.70830</td>\n      <td>144.9158</td>\n      <td>8870.0</td>\n    </tr>\n    <tr>\n      <th>6043</th>\n      <td>h</td>\n      <td>S</td>\n      <td>Western Metropolitan</td>\n      <td>3</td>\n      <td>13.3</td>\n      <td>3020.0</td>\n      <td>3.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>673.0</td>\n      <td>673.0</td>\n      <td>1970.0</td>\n      <td>-37.76230</td>\n      <td>144.8272</td>\n      <td>4217.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.head()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [],
   "source": [
    "from sklearn.compose import ColumnTransformer\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "\n",
    "numerical_transformer = SimpleImputer(strategy='constant')\n",
    "\n",
    "categorical_transformer = Pipeline(\n",
    "    steps=[('imputer', SimpleImputer(strategy='most_frequent')), ('onehot', OneHotEncoder(handle_unknown='ignore'))])\n",
    "\n",
    "preprocessor = ColumnTransformer(\n",
    "    transformers=[\n",
    "        ('num', numerical_transformer, numerical_cols),\n",
    "        ('cat', categorical_transformer, categorical_cols)\n",
    "    ]\n",
    ")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestRegressor\n",
    "\n",
    "model = RandomForestRegressor(n_estimators=100, random_state=0)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MAE: 160679.18917034855\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import mean_absolute_error\n",
    "\n",
    "my_pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('model', model)])\n",
    "\n",
    "my_pipeline.fit(X_train,y_train)\n",
    "\n",
    "preds = my_pipeline.predict(X_valid)\n",
    "\n",
    "score = mean_absolute_error(y_valid,preds)\n",
    "\n",
    "print('MAE:',score)"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
